// packageE/stationNotice/stationNoticeCustomer/stationNoticeCustomer.js

const app = getApp();
import dateParserTime from "../dateParserTime";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数

    listData: [],
    msg_body: {
      title: "",
      nickname: "会员昵称:",
      pay_time: "付款时间:",
      order_sn: "订单编号:",
      goodsName: "商品名:",
      pay_money: "订单金额:",
    },
    subTypeList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSubType();
  },
  //获取二级菜单
  async getSubType() {
    let urlStr = app.getNetAddresss(
      "plugin.instation-message.api.message.get-sub-type"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        type_id: 4,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        this.setData({ subTypeList: res.data });
        this.getListData();
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  gotoPages(evt) {
    let page = evt.currentTarget.dataset.page;
    if (page == "" || page == null) return;
    wx.navigateTo({
      url: page,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  initData() {
    this.data.page = 1;
    this.data.isLoadMore = true;
    this.data.total_page = 0;
  },
  async getListData() {
    this.initData();
    let urlStr = app.getNetAddresss(
      "plugin.instation-message.api.message.get-type-list"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        type_id: 4,
        sub_type: this.data.subTypeList[this.data.active].sub_type,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);

        res.data.data.forEach((element) => {
          element.created_at = dateParserTime(element.created_at);
        });

        this.data.isLoadMore = true;
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData: res.data.data,
        });
      },
    });
  },
  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss(
        "plugin.instation-message.api.message.get-type-list"
      );
      app._postNetWork({
        url: urlStr,
        data: {
          type_id: 4,
          sub_type: this.data.subTypeList[this.data.active].sub_type,
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            var nextPageData = res.data.data;
            let listData = this.data.listData.concat(nextPageData);
            nextPageData.forEach((element) => {
              element.created_at = dateParserTime(element.created_at);
            });
            this.setData({ listData });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  changeTags(e) {
    console.log(e.detail.index, this.data.active);
    this.data.active = e.detail.index;
    this.getListData();
  },

  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 2000,
    });
    return false;
  },
});
